package com.example.module_home.fragment

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.PagerSnapHelper
import com.example.lib_base.BaseFragment
import com.example.module_home.BR
import com.example.module_home.R
import com.example.module_home.adapter.CommonAdapter
import com.example.module_home.adapter.CommonViewHolder
import com.example.module_home.databinding.FragmentRecommendBinding
import com.example.module_home.databinding.VideoItemBinding
import com.example.module_home.entity.CommentEntity
import com.example.module_home.entity.RecommendVideoEntity
import com.example.module_home.intent.HomeIntent
import com.example.module_home.state.HomeState
import com.example.module_home.viewmodel.HomeViewModel
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch


class RecommendFragment : BaseFragment<FragmentRecommendBinding>() {
    private val viewModel by lazy{ HomeViewModel() }
    private val adapter2 by lazy{CommonAdapter<CommentEntity>({R.layout.comment_item to BR.comment})}
    private val adapter by lazy{ object :CommonAdapter<RecommendVideoEntity>({R.layout.video_item to BR.video}){
        override fun onBindViewHolder(holder: CommonViewHolder, position: Int) {
            super.onBindViewHolder(holder, position)
            val videoItemBinding = holder.binding as VideoItemBinding
            videoItemBinding.commentRec.adapter=adapter2
            videoItemBinding.videoGsy.setUp(data[position].videopath,true,data[position].title)
            videoItemBinding.videoGsy.startPlayLogic()
            videoItemBinding.closeComment.setOnClickListener {
                videoItemBinding.comment.visibility=View.GONE
            }
            videoItemBinding.openList.setOnClickListener {
                videoItemBinding.comment.visibility=View.VISIBLE
                lifecycleScope.launch {
                    viewModel.channel.send(HomeIntent.GetComment(0,"7055998662333334046"))
                }

            }
        }
    } }

    override fun initData() {
        lifecycleScope.launch {
            viewModel.observerState.collect{
                when (it){
                    is HomeState.RVideoSuccess->{
                        adapter.clear()
                        adapter+=it.list
                    }
                    is HomeState.CommentSuccess->{
                        adapter.clear()
                        adapter2+=it.list

                    }
                    else->{

                    }
                }
            }
        }
    }

    override fun initView() {
        MaterialAlertDialogBuilder(requireContext())
            .setMessage("个人信息保护指引\n" +
                    "1.我们会遵循隐私协议手机，使用信息，但不会因同意本隐私协议页采用强制捆绑得方式手机信息;\n" +
                    "2.为帮助服务所必需，我们将请求手机设备信息与地理位置用于用户视频和信息推送;\n" +
                    "3.为了帮你发现更多好友，在通讯录，GPS，摄像头，麦克风，相册权限均不会默认开启，不会在功能或服务不需要时而通过您授权授权得权限手机信息。\n" +
                    "查看完整版隐私权限")
            .setPositiveButton("我知道了",null)
            .show()
        binding.homeRec.adapter=adapter
        lifecycleScope.launch {
            viewModel.channel.send(HomeIntent.GetRVideo(1,10))
        }
        PagerSnapHelper().attachToRecyclerView(binding.homeRec)
    }

    override fun getLayoutId(): Int =R.layout.fragment_recommend

}